package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.dialog.ItemPopWindow;
import sz.szsmk.citizencard.ui.adapter.HomePageAdapter;
import sz.szsmk.citizencard.ui.dzsmkfragment.BillFragment;
import sz.szsmk.citizencard.ui.dzsmkfragment.MineFragment;
import sz.szsmk.citizencard.widget.NoScrollViewPager;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/6 19:57
 * description:
 */
@Route("dzsmkuser")
public class DzsmkUserActivity extends AppBaseActivity implements View.OnClickListener {


    NoScrollViewPager vpContent;
    private BillFragment billFragment;
    private MineFragment mineFragment;
    private TextView topview;

    private ArrayList<Fragment> fragments = new ArrayList<>();

//    private List<Fragment> fragments;

    ImageView iv_qr_right;
    TextView tv_qr_right;
    private ViewPager mViewPager;
    private int select = 0;


    @Override
    protected CharSequence getTopTitle() {
        return "";
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏ActionBar
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//此FLAG可使状态栏透明，且当前视图在绘制时，从屏幕顶端开始即top = 0开始绘制，这也是实现沉浸效果的基础
//        }
        setContentView(R.layout.dzsmk_user_activity);

        vpContent = findViewById(R.id.vp_content);
        iv_qr_right = findViewById(R.id.iv_qr_right);
        tv_qr_right= findViewById(R.id.tv_qr_right);

//        topview = (TextView) findViewById(R.id.top_view);
//        ViewGroup.LayoutParams layoutParams = topview.getLayoutParams();
//        layoutParams.height = getStatusHeightPx(this);
//        topview.setLayoutParams(layoutParams);
//        topview.setBackgroundColor(Color.TRANSPARENT);
        iv_qr_right.setVisibility(View.VISIBLE);
        iv_qr_right.setOnClickListener(this);
        tv_qr_right.setOnClickListener(this);
        String type = getIntent().getStringExtra("type");
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        mViewPager = (ViewPager) findViewById(R.id.vp_content);
        billFragment = new BillFragment();
        mineFragment = new MineFragment();
        fragments.add(billFragment);
        fragments.add(mineFragment);


        HomePageAdapter pageAdapter = new HomePageAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(pageAdapter);
        if (type.equals("1")) {
            setTopTitle("我的钱包");
            mViewPager.setCurrentItem(1);
        } else {
            setTopTitle("交易记录");
            mViewPager.setCurrentItem(0);
        }

        int currentItem = mViewPager.getCurrentItem();
        if (currentItem == 1) {
            iv_qr_right.setVisibility(View.GONE);
            tv_qr_right.setVisibility(View.VISIBLE);
        } else {
            iv_qr_right.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_qr_right) {
            showFilterPopDialog();
        } else if (id == R.id.tv_qr_right) {
            setTopTitle("交易记录");
            mViewPager.setCurrentItem(0);
//                Bundle bundle = new Bundle();
//                bundle.putString("type", "0");
//                JumpUtils.jumptoNatvie(this, "dzsmkuser", 0, bundle);
        }
    }

    private void showFilterPopDialog() {
        ItemPopWindow.PopItem popItem = new ItemPopWindow.PopItem();
        popItem.setName("全部");
        popItem.setPosition(0);

        ItemPopWindow.PopItem popItem2 = new ItemPopWindow.PopItem();
        popItem2.setName("消费");
        popItem2.setPosition(1);

        ItemPopWindow.PopItem popItem3 = new ItemPopWindow.PopItem();
        popItem3.setName("充值");
        popItem3.setPosition(2);

        List<ItemPopWindow.PopItem> popItems = new ArrayList<>();
        popItems.add(popItem);
        popItems.add(popItem2);
        popItems.add(popItem3);

        ItemPopWindow popWindow = new ItemPopWindow(DzsmkUserActivity.this, new ItemPopWindow.OnItemClickListener() {
            @Override
            public void onItemClick(ItemPopWindow.PopItem popItem) {
                if (0 == popItem.getPosition()) { //1 取消后，还在当前页面，恢复
                    select = 0;
                    billFragment.initData(2);
                } else if (1 == popItem.getPosition()) {
                    select = 1;
                    billFragment.initData(1);
                } else if (2 == popItem.getPosition()) {
                    select = 2;
                    billFragment.initData(0);
                }
            }
        }, popItems, select);

        popWindow.showAtLocation(findViewById(R.id.ll_qr_main),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
//        mMainFragment.setMainFragmentVisible(false);
    }
}
